#include "lm_misc.h"
#include "../common/lm_log.h"
#include "../common/lm_mem.h"


lm_win_t* lm_create_hroll_bar(lm_win_t *parent, uint32_t pos_x, uint32_t pos_y, uint32_t width, lm_color_t color, double roll_rate)
{
    lm_win_t* hroll_bar = lm_win_create(parent, pos_x, pos_y, width, 1, color);
    if(hroll_bar == NULL) {
        LM_LOG_ERROR("The hroll window alloc failed");
        return NULL;
    }

    hroll_bar->width = width;
    werase(hroll_bar->body);
    mvwaddnstr(hroll_bar->body, 0, roll_rate * (width-1), "\xDB", 1);
    wrefresh(hroll_bar->body);

    return hroll_bar;
}


void hroll_bar_set_value(lm_win_t *hroll_bar, double roll_rate)
{
    werase(hroll_bar->body);
    mvwaddnstr(hroll_bar->body, 0, roll_rate * (hroll_bar->width-1), "\xDB", 1);
    wrefresh(hroll_bar->body);
}

lm_win_t* lm_create_vroll_bar(lm_win_t *parent, uint32_t pos_x, uint32_t pos_y, uint32_t height, lm_color_t color, double roll_rate)
{
    lm_win_t* vroll_bar = lm_win_create(parent, pos_x, pos_y, 1, height, color);
    if(vroll_bar == NULL) {
        return NULL;
    }

    werase(vroll_bar->body);
    mvwaddnstr(vroll_bar->body, roll_rate*(height-1), 0, "\xDB", 1);
    wrefresh(vroll_bar->body);
    return vroll_bar;
}

void lm_vroll_set_value(lm_win_t *vroll_bar, double roll_rate)
{
    werase(vroll_bar->body);
    mvwaddnstr(vroll_bar->body, roll_rate * (vroll_bar->height-1), 0, "\xDB", 1);
    wrefresh(vroll_bar->body);
}


void lm_progress_bar_set_value(lm_win_t* progress_bar, double roll_rate)
{
    char *rate_cache = lm_malloc(progress_bar->width + 1);
    if(rate_cache == NULL) {
        return;
    }

    int32_t pos = progress_bar->width * roll_rate;

    memset(rate_cache, 0, progress_bar->width + 1);
    sprintf(rate_cache + progress_bar->width / 2 - 2, "%d%%", (int)(roll_rate * 100));

    werase(progress_bar->body);
    wattron(progress_bar->body, A_BOLD);

    short pair_tmp;

    if(progress_bar->color.bg != 0 && progress_bar->color.fg != 0) {
        pair_tmp = lm_init_pair(progress_bar->color.bg, progress_bar->color.fg);
    }
    else {
        pair_tmp = lm_init_pair(progress_bar->color.rgb_bg_index, progress_bar->color.rgb_fg_index);
    }

    for(int i = 0; i < pos; i++) {
        if(*(rate_cache + i) == 0) {
            wattrset(progress_bar->body, COLOR_PAIR(progress_bar->color.pair));
            mvwaddstr(progress_bar->body, 0, i, "\xDB");
        }
        else {
            wattrset(progress_bar->body, COLOR_PAIR(pair_tmp));
            mvwaddnstr(progress_bar->body, 0, i, rate_cache + i, 1);
        }
    }

    for(int i = pos; i < progress_bar->width; i++) {
        if(*(rate_cache + i) != 0) {
            wattrset(progress_bar->body, COLOR_PAIR(progress_bar->color.pair));
            mvwaddnstr(progress_bar->body, 0, i, rate_cache+i, 1);
        }
    }

    wrefresh(progress_bar->body);
    wattron(progress_bar->body, A_NORMAL);
    lm_free(rate_cache);
}


lm_win_t* lm_create_progress_bar(lm_win_t *parent, uint32_t pos_x, uint32_t pos_y, uint32_t length, lm_color_t color, double roll_rate)
{
    lm_win_t* progress_bar = lm_win_create(parent, pos_x, pos_y, length, 1, color);
    if(progress_bar == NULL) {
        return NULL;
    }

    lm_progress_bar_set_value(progress_bar, roll_rate);

    return progress_bar;
}
